﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class WarehouseService
    {
        /// <summary>
        /// 查询仓库信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="warehouseno"></param>
        /// <returns></returns>
        public Warehouse GetWarehouse(long id, String warehouseno)
        {
            Warehouse warehouse = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    warehouse = new Warehouse();
                    warehouse.Id = id;
                    warehouse.Warehouseno = warehouseno;
                    warehouse = new WarehouseDAO().GetWarehouse(conn, warehouse);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return warehouse;
        }

        /// <summary>
        /// 查询仓库信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="name"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <returns></returns>
        public DataTable GetWarehouseTable(int pageindex, int pagesize, String name, String province, String city)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Warehouse warehouse = new Warehouse();
                    warehouse.Pageindex = pageindex;
                    warehouse.Pagesize = pagesize;
                    warehouse.Name = name;
                    warehouse.Province = province;
                    warehouse.City = city;
                    dt = new WarehouseDAO().GetWarehouseTable(conn, warehouse);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询仓库记录数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <returns></returns>
        public long GetWarehouseRecord(String name, String province, String city)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Warehouse warehouse = new Warehouse();
                    warehouse.Name = name;
                    warehouse.Province = province;
                    warehouse.City = city;
                    record = new WarehouseDAO().GetWarehouseRecord(conn, warehouse);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入仓库信息
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public long InsertWarehouse(Warehouse warehouse)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new WarehouseDAO().InsertWarehouse(conn, warehouse);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新仓库信息
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public int UpdateWarehouse(Warehouse warehouse)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new WarehouseDAO().UpdateWarehouse(conn, warehouse);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除仓库信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteWarehouse(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        WarehouseDAO warehouseDAO = new WarehouseDAO();
                        Warehouse warehouse = new Warehouse();
                        foreach (String lopid in arr)
                        {
                            warehouse.Id = Int64.Parse(lopid);
                            warehouseDAO.DeleteWarehouse(conn, warehouse);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
